package com.sec.android.app.myfiles.decorator;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment.ActionModeType;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.IMenuImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;

public class MenuImpDecorator implements IMenuImp {
    protected final Context mContext;
    private final IMenuImp mOrgMenuImp;

    public MenuImpDecorator(Context context, IMenuImp menuImp) {
        mOrgMenuImp = menuImp;
        mContext = context;
    }

    @Override
    public void setMenuRes(int menuResId) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setMenuRes(menuResId);
        }
    }

    @Override
    public void setActionMenuRes(int menuResId) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setActionMenuRes(menuResId);
        }
    }

    @Override
    public void setInfo(AbsMyFilesFragment fragment, NavigationInfo navigationInfo) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setInfo(fragment, navigationInfo);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater, ActionModeType actionMode) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onCreateOptionsMenu(menu, inflater, actionMode);
        }
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu, ActionModeType actionMode) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onPrepareOptionsMenu(menu, actionMode);
        }
    }

    @Override
    public void onPrepareActionMenu(Menu menu, ActionModeType actionMode) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onPrepareActionMenu(menu, actionMode);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean ret = false;
        if (mOrgMenuImp != null) {
            ret = mOrgMenuImp.onOptionsItemSelected(item);
        }
        return ret;
    }

    @Override
    public void setActionBarNormal(ActionBar actionbar) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setActionBarNormal(actionbar);
        }
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        boolean ret = false;
        if (mOrgMenuImp != null) {
            ret = mOrgMenuImp.onActivityResult(requestCode, resultCode, data);
        }
        return ret;
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onPrepareNormalOptionsMenu(menu);
        }
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onPrepareEditOptionsMenu(menu);
        }
    }

    @Override
    public void setMenuItemVisibility(Menu menu, int resMenuId, boolean visibility) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setMenuItemVisibility(menu, resMenuId, visibility);
        }
    }

    @Override
    public void setMenuItemEnabled(Menu menu, int resMenuId, boolean enabled) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.setMenuItemEnabled(menu, resMenuId, enabled);
        }
    }

    @Override
    public String getSelectedItemString(int selectedCnt) {
        String ret = "";
        if (mOrgMenuImp != null) {
            ret = mOrgMenuImp.getSelectedItemString(selectedCnt);
        }
        return ret;
    }

    @Override
    public void onPause() {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onPause();
        }
    }

    @Override
    public void onCreateContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {
        if (mOrgMenuImp != null && record != null) {
            if (fragment != null && !fragment.isSelectActionMode()) {
                menu.setHeaderTitle(getHeaderTitle(fragment, record));
            }
            mOrgMenuImp.onCreateContextualMenu(menu, fragment, record);
        }
    }

    private String getHeaderTitle(AbsMyFilesFragment fragment, FileRecord record) {
        String ret;
        int panelInfo = fragment.getPanelInfo();
        if (panelInfo == AppConstants.PanelInfo.LeftPanel || record.isRoot()) {
            ret = record.getDisplayName(mContext);
        } else {
            ret = record.getName();
        }
        return ret;
    }

    @Override
    public boolean onContextualItemSelected(MenuItem item, AbsMyFilesFragment fragment, FileRecord record) {
        return mOrgMenuImp != null && mOrgMenuImp.onContextualItemSelected(item, fragment, record);
    }

    @Override
    public void onUpdateSelectLayoutMenu(Menu menu, NavigationInfo navigationInfo) {
        if (mOrgMenuImp != null) {
            mOrgMenuImp.onUpdateSelectLayoutMenu(menu, navigationInfo);
        }
    }

    @Override
    public String getActionBarTitle() {
        String ret = "";
        if (mOrgMenuImp != null) {
            ret = mOrgMenuImp.getActionBarTitle();
        }
        return ret;
    }
}
